<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class PhotoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $imgs = DB::table("imgs")->paginate(5);
        $count=DB::table('imgs')->count('id');
        return view("admin/photo.photo_index")->with("imgs",$imgs)->with('count',$count);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //普通form 文件上传
          return view("admin/photo.photo_add");
           
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       // //单图片上传
       // $img = $request->file('img');
       // if($img->isValid()){
       //     $ext = $img->getClientOriginalExtension(); //获取上传文件名的后缀名
       //     $path = public_path('upload/images/');
       //     $name = "origin_".time().rand(10,100).'.'.$ext;
       //     $time = date("Y-m-d H:i:s"); 
       //     if($img->move($path,$name)){
       //         $url = '/upload/images/'.$name;
       //         DB::table('imgs')->insert(['url'=>$url,'created_at'=>$time]);
       //       return redirect('/photo'); 
       //     }
       // }
       // 
       //多图上传
       $imgs = $request->file('image');
        // dd($imgs);
       $urls = [];
       foreach($imgs as $img){
           if($img->isValid()){
               $ext = $img->getClientOriginalExtension(); //获取上传文件名的后缀名
               $path = public_path('upload/images/');
               $name = "origin_".time().rand(10,100).'.'.$ext;
               $time = date("Y-m-d H:i:s"); 

               if($img->move($path,$name)){
                   $url = '/upload/images/'.$name;
                   $urls[] = ['url'=>$url,'created_at'=>$time];

                    
                }
            }
       }
       //返回ajax
        $data = ['status' => 200, 'msg' => '成功', 'data' => $urls];
       echo json_encode($data);
      $res =  DB::table('imgs')->insert($urls);
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function delete($id)
    {
        $data=DB::table('imgs')->where('id',$id)->delete();
        
            
            return 1;        
    }
}
